package com.web.servlet.bookrack;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.web.bean.bookrack.BookRack;
import com.web.bean.user.User;
import com.web.dao.bookrack.BookRackDaoImpl;
import com.web.dao.user.UserDao;
import com.web.result.Result;
import com.web.service.BookRack.BookRackService;
import com.web.service.BookRack.BookRackServiceImpl;
import com.web.utils.DBUtil;
import com.web.utils.JSONWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.Timestamp;
import java.util.List;

/**
 * @autor Chen
 * @create 2021--28-10:59
 * @desc
 */
@WebServlet("/bookrack/*")
public class BookRackServlet extends HttpServlet {


    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        String uri = req.getRequestURI().replaceAll("/bookrack/","");
        if ("addHistory".equals(uri)) addHistoryBookRack(req,resp);
        if ("showHistoryBookRacks".equals(uri)) showHistoryBookRacks(req,resp);
        if ("showBookRacks".equals(uri)) showBookRacks(req,resp);
        if ("updateBookRackIn".equals(uri)) updateBookRackIn(req,resp);
        if ("updateBookRackOut".equals(uri)) updateBookRackOut(req,resp);
        if ("deleteHistoryBookRack".equals(uri)) deleteHistoryBookRack(req,resp);
    }

    //curl -X POST http://127.0.0.1:8081/bookrack/deleteHistoryBookRack -d "nid=N1"
    private void deleteHistoryBookRack(HttpServletRequest req, HttpServletResponse resp) {
        Result result =new Result();
        DBUtil dbUtil =DBUtil.getInstance();
        BookRackServiceImpl bookRackService =new BookRackServiceImpl();
        try {
            bookRackService.deleteHistoryBookRack(req.getParameter("nid"));
            result.setFlag(true);
            result.setMessage("移除历史记录成功");
        } catch (Exception e) {
            result.setFlag(false);
            result.setMessage(e.getMessage());
        }finally {
            try {
                dbUtil.close();// 关闭数据库
                PrintWriter pw = resp.getWriter();
                pw.println(JSON.toJSONString(result));
                pw.flush();
                pw.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    //curl -X POST http://127.0.0.1:8081/bookrack/updateBookRackOut -d "nid=N1"
    private void updateBookRackOut(HttpServletRequest req, HttpServletResponse resp) {
        Result result =new Result();
        DBUtil dbUtil =DBUtil.getInstance();
        try{
            BookRack bookRack =new BookRack();
            BookRackDaoImpl bookRackDao =new BookRackDaoImpl();
            bookRack = bookRackDao.selectBookRackById(req.getParameter("nid"));
            bookRack.setBstatus(0);
            bookRack.setHisTime(new Timestamp(System.currentTimeMillis()));
            BookRackServiceImpl bookRackService =new BookRackServiceImpl();
            bookRackService.updateBookRackIn(bookRack);
            result.setFlag(true);
            result.setMessage("移除书架成功");
        }catch (Exception e){
            result.setFlag(false);
            result.setMessage(e.getMessage());
        }finally {
            try {
                dbUtil.close();// 关闭数据库
                PrintWriter pw = resp.getWriter();
                pw.println(JSON.toJSONString(result));
                pw.flush();
                pw.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    //curl -X POST http://127.0.0.1:8081/bookrack/updateBookRackIn -d "nid=N1"
    private void updateBookRackIn(HttpServletRequest req, HttpServletResponse resp) {
        Result result =new Result();
        DBUtil dbUtil =DBUtil.getInstance();
        try{
            BookRack bookRack =new BookRack();
            BookRackDaoImpl bookRackDao =new BookRackDaoImpl();
            bookRack = bookRackDao.selectBookRackById(req.getParameter("nid"));
            bookRack.setBstatus(1);
            bookRack.setHisTime(new Timestamp(System.currentTimeMillis()));
            BookRackServiceImpl bookRackService =new BookRackServiceImpl();
            bookRackService.updateBookRackIn(bookRack);
            result.setFlag(true);
            result.setMessage("添加书架成功");
        }catch (Exception e){
            result.setFlag(false);
            result.setMessage(e.getMessage());
        }finally {
            try {
                dbUtil.close();// 关闭数据库
                PrintWriter pw = resp.getWriter();
                pw.println(JSON.toJSONString(result));
                pw.flush();
                pw.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    //curl -X POST http://127.0.0.1:8081/bookrack/showBookRacks
    private void showBookRacks(HttpServletRequest req, HttpServletResponse resp) {
        Result result = new Result();
        DBUtil dbUtil = DBUtil.getInstance();
        try {
            BookRackServiceImpl bookRackService =new BookRackServiceImpl();
            List<BookRack> bookRacks = bookRackService.showBookRacks();
            result.setFlag(true);
            result.setMessage("书架查询成功");
            result.setData(bookRacks);

        } catch (Exception e) {
            result.setFlag(false);
            result.setMessage(e.getMessage());
        } finally {
            JSONWriter.writeJSON(result,resp);
        }

    }

    //curl -X POST http://127.0.0.1:8082/bookrack/showHistoryBookRacks
    private void showHistoryBookRacks(HttpServletRequest req, HttpServletResponse resp) {
        Result result = new Result();
        DBUtil dbUtil = DBUtil.getInstance();
        try {
            BookRackServiceImpl bookRackService =new BookRackServiceImpl();
            List<BookRack> bookRacks = bookRackService.showHistoryBookRacks();
            result.setFlag(true);
            result.setMessage("查询成功");
            result.setData(bookRacks);
        } catch (Exception e) {
            result.setFlag(false);
            result.setMessage(e.getMessage());
        } finally {
            try {
                dbUtil.close();
                PrintWriter pw = resp.getWriter();
                pw.println(JSON.toJSONStringWithDateFormat(
                        result,
                        "yyyy-MM-dd HH:mm:ss",
                        SerializerFeature.WriteDateUseDateFormat)
                );
                pw.flush();
                pw.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    //curl -X POST http://127.0.0.1:8081/bookrack/addHistory -d "uid=U1111&nid=N11111&nname='校花'&chapterId=500"
    private void addHistoryBookRack(HttpServletRequest req, HttpServletResponse resp) {
        Result result =new Result();
        DBUtil dbUtil =DBUtil.getInstance();
        //解析数据
        /*
        *  String uid;//用户id
        *  String nid;//书id
        *  String nname;//书名
        *  int bstatus;//状态
        *  String chapterId;//书章节
        *  Timestamp hisTime;//点击的时间
        *
        * */
        try{
            String uid = req.getParameter("uid");
            String nid = req.getParameter("nid");
            String nname = req.getParameter("nname");
            String chapterId = req.getParameter("chapterId");
            //封装对象
            BookRack bookRack =new BookRack();
            bookRack.setUid(uid);
            bookRack.setNid(nid);
            bookRack.setNname(nname);
            bookRack.setBstatus(0);
            bookRack.setChapterId(chapterId);
            bookRack.setHisTime(new Timestamp(System.currentTimeMillis()));
            BookRackService bookRackService =new BookRackServiceImpl();
            bookRackService.addHistoryBookRack(bookRack);
            //
            result.setFlag(true);
            result.setMessage("新增成功");
        }catch (Exception e){
            result.setFlag(false);
            result.setMessage(e.getMessage());
        }finally {
            try {
                dbUtil.close();// 关闭数据库
                PrintWriter pw = resp.getWriter();
                pw.println(JSON.toJSONString(result));
                pw.flush();
                pw.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
